package fw.helpers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fw.ApplicationManager;

public class ErrorsHelper {
	private final ApplicationManager app;
	private static Logger LOG = LoggerFactory.getLogger(ErrorsHelper.class);

	public ErrorsHelper(ApplicationManager p_app) {
		app = p_app;
	}

	/**
	 * Set error message without params
	 * 
	 * @param p_errorMessage
	 */
	public void set(String p_errorMessage) {
		set(p_errorMessage, "");
	}

	/**
	 * Includes errors and test parameters to error message set error message to LOG to console
	 * 
	 * @param p_errorMessage
	 * @param p_params
	 */
	public void set(String p_errorMessage, String... p_params) {
		String error = includeParams(p_errorMessage, p_params);

		LOG.error(error);
	}

	/**
	 * Includes errors and test parameters to error message do not set error message to LOG to console
	 * 
	 * @param p_errorMessage
	 * @param p_params
	 * @return String
	 */
	public String includeParams(String p_errorMessage, String... p_params) {
		String lineSeparator = System.getProperty("line.separator");
		String error = "";
		String errorParam = "";
		String errorMessage = "";

		errorMessage = "Failed: " + p_errorMessage + "[";
		app.errorCollector += lineSeparator + errorMessage;
		error += errorMessage;

		if (p_params != null) {
			for (Integer i = 0; i < p_params.length; i++) {
				if (i.equals(p_params.length - 1)) {
					errorParam = p_params[i] + "]";
				} else {
					errorParam = p_params[i] + ",";
				}

				error += errorParam;
				app.errorCollector += errorParam;
			}
		}

		return error;
	}

	/**
	 * Clear error collector
	 */
	public void clear() {
		app.errorCollector = "";
	}

	/**
	 * Get errors from error collector
	 * 
	 * @return String
	 */
	public String get() {
		return app.errorCollector;
	}

}
